package com.liu.email;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * 白名单发送邮件任务
 *
 * @author 刘波
 */
public class Text {
//    public static void main(String[] args) {
//        start();
//    }

    /**
     * 执行发送邮件方法
     */
    public static void start() {
        Map<String, Mail> mails = null;

        // 获取待发送的白名单
        try {
            mails = handleDataListNull();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            // 将告警数据转换成邮件格式
            mails.get("whiteMail").setTo("dasdaadsadasd@qq.com;1299616634@qq.com;liubo20180827@cnsunet.com");
            // 发送邮件
            sendMail(mails.get("whiteMail"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 将数据转换成邮件格式
     *
     * @param
     * @param
     * @return
     * @throws UnsupportedEncodingException
     */
    private static Map<String, Mail> handleDataListNull() throws UnsupportedEncodingException {
        Map<String, Mail> mails = new HashMap<String, Mail>();
        Mail whiteMail = new Mail();
        whiteMail.setTo("");
        whiteMail.setCC("");
        whiteMail.setBCC("");
        whiteMail.setSubject("刘波");
        whiteMail.setContent("liub@cnsunet.com;1299616634@qq.com;liubo20180827@cnsunet.com");
        // 放入MAP中，以所有告警ID为键值
        mails.put("whiteMail", whiteMail);
        return mails;
    }

    /**
     * 获取SMTP服务器设置，并发送邮件
     *
     * @param mail
     * @return
     */
    private static boolean sendMail(Mail mail) {
        String serverAddr = "smtp.qq.com";
        String serverPort = "25";
        String recUserName = "1299616634@qq.com";
        String recUserPwd = "kiwjgnnajkcjhdja";
        String fromMailAddr = "1299616634@qq.com";
        boolean needAuth = true;
        boolean needSsl = false;

        SimpleEmail email = null;
        System.out.println("To:" + mail.getTo() + " CC:" + mail.getCC() + " BCC:" + mail.getBCC());
        String[] to = mail.getTo().split(";");
        for (int i = 0; i < to.length; i++) {
            email = new SimpleEmail();
            email.setTLS(needSsl);
            email.setHostName(serverAddr);
            email.setSmtpPort(Integer.parseInt(serverPort));
            if (needAuth) {
                // 用户名和密码
                email.setAuthentication(recUserName, recUserPwd);
            }
            try {
                // 接收方
                email.addTo(to[i]);
                email.setFrom(fromMailAddr);
                email.setCharset("GB2312");
                // 标题
                email.setSubject(mail.getSubject());
                // 内容
                email.setMsg(mail.getContent());
                email.send();
            } catch (EmailException e) {
                System.out.println(e);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        return true;
    }
}
